﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TelerikedIn.Data;
using TelerikedIn.WebApp.ViewModels.Home;
using TelerikedIn.WebApp.ViewModels.User;
using Microsoft.AspNet.Identity;
using Kendo.Mvc.UI;

namespace TelerikedIn.WebApp.Controllers
{
    public class HomeController : Controller
    {
        private UowData data;

        public HomeController()
        {
            this.data = new UowData();
        }

        public HomeController(UowData data)
        {
            this.data = data;
        }

        public ActionResult Index()
        {
            PublicInfoVM info = this.GetPublicInfo();
            return View(info);
        }

        private PublicInfoVM GetPublicInfo()
        {
            var users = this.data.Users.All().OrderBy(u => Guid.NewGuid());
            var corrUserId = User.Identity.GetUserId();
            var publicInfoVM = new PublicInfoVM
            {
                UsersCount = users.Count(),
                SkillsCount = this.data.Skills.All().Count(),
                UsersYouMayKnow = users.Where(u => u.Id != corrUserId).Take(5).Select(UserVM.FromUser)
            };

            return publicInfoVM;
        }

        public ActionResult AutoComplete(string text)
        {
            var users = this.data.Users.All()
                .Where(u => (u.FirstName.ToLower() + " " + u.LastName.ToLower()).Contains(text.ToLower()))
                .Select(u => new UserVM()
            {
                FullName = u.FirstName + " " + u.LastName,
                Id = u.Id
            });

            return Json(users, JsonRequestBehavior.AllowGet);
        }
    }
}